// ActionScript file
import com.helloniu.comp.RemoteVideo;
import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.Tool;
import com.helloniu.fl.widget.HNCoreCompnent;

private var ncHandler:NCHandler = null;
private var soHandler:SOHandler = null;
private var _userID:String = null;

public function initConnection(ancHandler:NCHandler, asoHandler:SOHandler, auserID:String):void {
	this.ncHandler = ancHandler;
	this.soHandler = asoHandler;
	this._userID = auserID;
	
	this.soHandler.addChangeListener(statusChange);
	// @by tonyy, in view stack, if this panel is not default shown, the children is null 
	// initCoreComp();
}

private function initCoreComp(e:*):void {    
	var coreComps:Array = [videoPanel1, videoPanel2, videoPanel3, videoPanel4, 
		videoPanel5, videoPanel6, videoPanel7, videoPanel8];
Tool.initConnection(coreComps, ncHandler, soHandler, _userID);
/*
	for(var i:int = 0; i < coreComps.length; i++) {
    	var coreComp:HNCoreCompnent = coreComps[i] as HNCoreCompnent;

    	if(coreComp == null) {
    		Tool.alert("===video panel error found==");
        	return;
        }

    	coreComp.initConnection(ncHandler, soHandler, _userID);
    }    		
    */
}

private function statusChange(asoHandler:SOHandler, pname:String = null, 
	oldValue:* = null, newValue:* = null):void 
{
    if(pname == null) {
        return;
    }

    newValue = asoHandler.getProperty(pname);

	if(pname == "UpdateBussinessUnit") {
        //newValue
        var str:String = newValue + "";
    	var arr:Array = str.split(":");
    	
    	if(arr[1] == "video") {
    		updatePlayVideo(arr[0], arr[1], arr[2]);
    	}    
    }
}

private function updatePlayVideo(uid:String, key:String, value:*):void {
	var playName:String = null; 
	
	if(value == "true") {
		playName = uid + "avstream";
	}

	var playVideos:Array = [videoPanel2, videoPanel3, videoPanel4, 
		videoPanel5, videoPanel6, videoPanel7, videoPanel8];
	var isNewPublishUser:Boolean = false;
	
	for(var i:int = 0; i < playVideos.length; i++) {
    	var playVideo:RemoteVideo = playVideos[i] as RemoteVideo;

		if(playVideo.getPlayUserID() == null) {
			playVideo.setPlayUserID(uid);
			isNewPublishUser = true;
		}

		if(playVideo.getPlayUserID() == uid) {
			playVideo.setPlayStreamName(playName);
			isNewPublishUser = true;
		}
		
		if(isNewPublishUser) {
			break;;
		}	
	}
}

// setPVSName(sobj.name + "avstream");